﻿using Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Project_V1
{
    public partial class FrmIOVarManage : Form
    {
        public FrmIOVarManage()
        {
            InitializeComponent();
            this.Load += FrmIOVarManage_Load;
            this.dgv_data.AutoGenerateColumns = false;
        }

        #region 窗体Load加载时读取XML文件到集合,刷新DGV
        void FrmIOVarManage_Load(object sender, EventArgs e)
        {
            this.VarList = CommonMethods.LoadXML(CommonMethods.VarFilePath);
            UpdateDGV();
        }
        #endregion

        //ADS变量List集合(刷新DGV,存入XML文件)
        private List<Variable_Ads> VarList = new List<Variable_Ads>();

        //创建保存完成标志位,用于完善点击保存按钮后的运行逻辑,(保存后不弹出提示框,未保存时候弹出对话框)
        private bool isSaved = false;

        #region 刷新DataGridView方法（集合VarList作为数据源刷新）
        private void UpdateDGV()
        {
            this.dgv_data.DataSource = null;
            this.dgv_data.DataSource = this.VarList;
        }
        #endregion

        #region 新建按钮(将对象信息添加到集合VarList,刷新DGV)
        //点击新建按钮打开新建界面,将界面的值传入Variable_Modbus对象var,新建界面点确认后,将对象信息添加到集合
        private void btn_New_Click(object sender, EventArgs e)
        {            
            Frm_NewVar_Mod objFrm = new Frm_NewVar_Mod();
            objFrm.ShowDialog();
            DialogResult dr = objFrm.DialogResult;
            if (dr == DialogResult.OK)
            {
                this.VarList.Add(objFrm.var);
            }
            UpdateDGV();
        }
        #endregion      

        #region 保存按钮(将集合信息遍历后保存到XML文件,刷新DGV)
        private void btn_Save_Click(object sender, EventArgs e)
        {            
            try
            {
                //保存Modbus变量到XML文件
                XmlDocument rootxml = new XmlDocument();
                XmlElement rootnode = rootxml.CreateElement("Root");
                foreach (Variable_Ads item in this.VarList)
                {
                    XmlElement xmle = rootxml.CreateElement("Variable");                   
                    CommonMethods.XMLAttributeAppend(rootxml, "VarName", item.VarName, xmle);
                    CommonMethods.XMLAttributeAppend(rootxml, "DataType", item.DataType, xmle);
                    CommonMethods.XMLAttributeAppend(rootxml, "Note", item.Note, xmle);
                    CommonMethods.XMLAttributeAppend(rootxml, "IsFiling", item.IsFiling, xmle);
                    CommonMethods.XMLAttributeAppend(rootxml, "IsAlarm", item.IsAlarm, xmle);
                    CommonMethods.XMLAttributeAppend(rootxml, "IsReport", item.IsReport, xmle);
                    rootnode.AppendChild(xmle);
                }
                rootxml.AppendChild(rootnode);

                if (File.Exists(CommonMethods.VarFilePath))//判断路径下是否存在该文件名
                {
                    File.Delete(CommonMethods.VarFilePath);
                }
                //保存XML文件在路径VarFilePath
                rootxml.Save(CommonMethods.VarFilePath);
            }

            catch (Exception ex)
            {
                MessageBox.Show("保存出错" + ex.Message, "保存提示");
                return;
            }

            MessageBox.Show("保存成功", "保存提示");
            isSaved = true;
            UpdateDGV();
        }

        #endregion

        #region 修改按钮(将DGV选中行的变量对象信息传入Frm_ModVar_Mod界面,修改VarList集合信息,刷新DGV)
        //点击修改按钮打开修改界面,将DGV选中行的变量对象信息传入界面,修改界面点确认后,将对象信息赋值到集合对应索引的对象
        private void btn_Modify_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.SelectedRows.Count > 0)
            {
                int index = this.dgv_data.CurrentRow.Index;
                Variable_Ads ModVar = this.VarList[index];//DGV显示数据的索引Index和集合的索引一一对应                
                //Frm_ModVar_Mod窗体构造方法重载,使用有参数构造方法传值给窗体
                Frm_ModVar_Mod objFrm = new Frm_ModVar_Mod(ModVar);
                DialogResult dr = objFrm.ShowDialog();
                //Frm_ModVar_Mod窗体内点击确定后窗体关闭,执行修改操作
                if (dr == DialogResult.OK)
                {
                    this.VarList[index] = objFrm.var;
                }
                UpdateDGV();
            }
        }
        #endregion

        #region 删除按钮(将DGV选中行的对象信息从VarList集合中删除,刷新DGV)
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (this.dgv_data.SelectedRows.Count > 0)
            {
                int index = this.dgv_data.CurrentRow.Index;//RemoveAt()在指定索引处删除
                this.VarList.RemoveAt(index);                
            }
            UpdateDGV();
        }
        #endregion

        #region 其他优化,包括双击修改,添加DGV行号,弹出提示保存对话框(新建后未点击保存不会存到XML文件)
        //双击dgv一栏实现对该栏的修改操作
        private void dgv_data_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btn_Modify_Click(null, null);
        }

        //dgv的RowPostPaint行绘制事件
        //添加类库文件DataGridViewStyle,为dgv表单添加行号
        private void dgv_data_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            new DataGridViewStyle().DgvRowPostPaint(this.dgv_data, e);
        }

        //窗体关闭事件,isSaved来判定是否弹出提示保存对话框
        private void FrmIOVarManage_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isSaved == false)
            {
                DialogResult result = MessageBox.Show("是否保存修改内容?", "退出询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Cancel)
                {
                    isSaved = true;
                    return;
                }
                else
                {
                    btn_Save_Click(null, null);
                }
            }
        }
        #endregion

    }
}
